{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2deb6162",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8757d5c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "dfs = pd.read_excel(\"学生成绩表.xlsx\", sheet_name=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c18037c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_all = pd.concat(dfs.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b500206a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>姓名</th>\n",
       "      <th>成绩</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>高102班</td>\n",
       "      <td>李静</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>高103班</td>\n",
       "      <td>张勇</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>高102班</td>\n",
       "      <td>王丽</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      班级  姓名  成绩\n",
       "0  高102班  李静  71\n",
       "2  高103班  张勇  65\n",
       "3  高102班  王丽  96"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_all.sample(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e351da54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>姓名</th>\n",
       "      <th>成绩</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">高101班</th>\n",
       "      <th>0</th>\n",
       "      <td>高101班</td>\n",
       "      <td>张伟</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>高101班</td>\n",
       "      <td>张敏</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>高101班</td>\n",
       "      <td>李娜</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">高102班</th>\n",
       "      <th>4</th>\n",
       "      <td>高102班</td>\n",
       "      <td>李强</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>高102班</td>\n",
       "      <td>张静</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>高102班</td>\n",
       "      <td>李敏</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">高103班</th>\n",
       "      <th>9</th>\n",
       "      <td>高103班</td>\n",
       "      <td>李娟</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>高103班</td>\n",
       "      <td>张磊</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>高103班</td>\n",
       "      <td>王勇</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            班级  姓名  成绩\n",
       "班级                    \n",
       "高101班 0  高101班  张伟  86\n",
       "      9  高101班  张敏  83\n",
       "      6  高101班  李娜  74\n",
       "高102班 4  高102班  李强  99\n",
       "      5  高102班  张静  99\n",
       "      6  高102班  李敏  97\n",
       "高103班 9  高103班  李娟  96\n",
       "      5  高103班  张磊  94\n",
       "      1  高103班  王勇  92"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_all.groupby([\"班级\"]).apply(lambda x : x.nlargest(3, \"成绩\")).to_exce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61dc18ad",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
